using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Collections.ObjectModel;
using MReviewerClient.MReviewerSvc;
using MReviewerClient.Providers;

namespace MReviewerClient.Models
{
    /// <summary>
    /// The model of Establishment
    /// </summary>
    public class EstablishmentModel
    {
        #region Fields
        
        /// <summary>
        /// The collection of Establishment
        /// </summary>
        public ObservableCollection<EstablishmentDTO> Establishments;

        /// <summary>
        /// The object for synchronization
        /// </summary>
        private static object syncLock = new object();

        /// <summary>
        /// The instance of EstablishmentModel
        /// </summary>
        private static EstablishmentModel instance;

        /// <summary>
        /// The singleton instance
        /// </summary>
        public static EstablishmentModel Instance
        {
            get
            {
                lock (syncLock)
                {
                    if (instance == null)
                        instance = new EstablishmentModel();
                    return instance;
                }
            }
        }

        /// <summary>
        /// The result of update
        /// </summary>
        public bool UpdateResult { get; set; }

        /// <summary>
        /// The result of insert
        /// </summary>
        public bool InsertResult { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Select all of Establishment from the service
        /// </summary>
        public void SelectAll()
        {
            // The action is invoked when the operation is complete
            EventHandler<Establishment_SelectAllCompletedEventArgs> handler = null;
            handler = (s, e) =>
            {
                this.Establishments = e.Result;
                MReviewerService.Instance.Establishment_SelectAllCompleted -= handler;
            };
            // Read all of the establishment
            MReviewerService.Instance.Establishment_SelectAllCompleted += handler;
            MReviewerService.Instance.Establishment_SelectAllAsync();
        }

        /// <summary>
        /// Select an Establishment by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public EstablishmentDTO SelectByID(string id)
        {
            return this.Establishments.FirstOrDefault(n => n.ID.Equals(id, StringComparison.OrdinalIgnoreCase));
        }

        /// <summary>
        /// Update information detail of the specified establishment
        /// </summary>
        /// <param name="item">
        /// The Establishment will be updated
        /// </param>
        /// <param name="actionInformResult">
        /// This action will be executed when the operation for interacting to service is completely
        /// </param>
        public void Update(EstablishmentDTO item, Action actionInformResult)
        {
            // The action is invoked when the operation is complete
            EventHandler<Establishment_UpdateCompletedEventArgs> handler = null;
            handler = (s, e) =>
            {
                this.UpdateResult = e.Result;
                MReviewerService.Instance.Establishment_UpdateCompleted -= handler;
                actionInformResult();
            };
            MReviewerService.Instance.Establishment_UpdateCompleted += handler;
            MReviewerService.Instance.Establishment_UpdateAsync(item);
        }

        /// <summary>
        /// Insert new establishment
        /// </summary>
        /// <param name="item">
        /// The establishment information for inserting
        /// </param>
        /// <param name="actionInformResult">
        /// This action will be executed when the operation for interacting to service is completely
        /// </param>
        public void Insert(EstablishmentDTO item, Action actionInformResult)
        {
            // The action is invoked when the operation is complete
            EventHandler<Establishment_InsertCompletedEventArgs> handler = null;
            handler = (s, e) =>
            {
                this.InsertResult = e.Result;
                MReviewerService.Instance.Establishment_InsertCompleted -= handler;
                actionInformResult();
            };
            MReviewerService.Instance.Establishment_InsertCompleted += handler;
            MReviewerService.Instance.Establishment_InsertAsync(item);
        }

        #endregion
    }
}